<?php
$app->group('/task',function() use($app,$c){
    //$app->applyHook('task.before');
    $app->get('/',function() use($app,$c){
        $table=$c['Task.Table.Todo'];
        $app->render('task/index.html',array('models'=>array()));
    })->name('task-home');
    $app->get('/task-list/:id',function() use($app){
        
    })->name('task-list');
    $app->get('/add',function() use($app){
        $app->render('task/add.html',array('models'=>array()));
    })->name('task-add');
    $app->post('/add',function() use($app,$c){
        $table=$c['Task.Table.Todo'];
        $content = $app->request->post('content', '');
        $title = $app->request->post('title', '');
        $now=new Zend\Db\Sql\Expression('now()');
        $table->insert(array(
            'title'=>$title,
            'content'=>$content,
            'user_id'=>81,
            'project_id'=>19,
            'done'=>0,
            'created_at'=>$now,
            'updated_at'=>$now
        ));
        $app->redirect($app->urlFor('task-add'), 302);
    });
    $app->get('/projects/:id/inbox',function($id) use($app){
        $project=$app->db->fetchOne('select id,name from task_project where id=:id',array('id'=>$id));
        $groups=$app->db->fetchAll('select id,name from task_group where project_id=:id',array('id'=>$id));
        $todos=$app->db->fetchAll('select id,title from task_todo where project_id=:id and deleted=0 and done=0 order by sort_order asc',array('id'=>$id));
        $app->render('projects/inbox',array('project'=>$project,'groups'=>$groups,'todolist'=>$todos));
    })->name('task-inbox');
    $app->get('/projects/:id/groups/:gid',function($id,$gid) use($app){
        $project=$app->db->fetchOne('select id,name from task_project where id=:id',array('id'=>$id));
        $groups=$app->db->fetchAll('select id,name from task_group where project_id=:id',array('id'=>$id));
        $todos=$app->db->fetchAll('select id,title from task_todo where group_id=:tid and deleted=0 and done=0 order by sort_order asc',array('tid'=>$tid));
        $app->render('projects/inbox',array('project'=>$project,'groups'=>$groups,'todolist'=>$todos));
    })->name('task-group');
    $app->get('/projects/:id/todos/:tid',function($id,$tid) use($app){
        $project=$app->db->fetchOne('select id,name from task_project where id=:id',array('id'=>$id));
        $groups=$app->db->fetchAll('select id,name from task_group where project_id=:id',array('id'=>$id));
        //根据tid获取gid,$todos=$app->db->fetchAll('select id,title from task_todo where group_id=:tid and deleted=0 and done=0 order by sort_order asc',array('tid'=>$tid));
        $todo=$app->db->fetchOne('select id,title,content from task_todo where id=:tid and deleted=0 and done=0',array('tid'=>$tid));
        $notes=$app->db->fetchAll('select * from task_note where task_id=:id',array('id'=>$tid));
        $app->render('projects/inbox',array('project'=>$project,'groups'=>$groups,'todolist'=>$todos,'todo'=>$todo,'notes'=>$notes));
    })->name('task-todo');
    $app->group('/api',function() use($app){
        $app->get('/tasks',function() use($app){

        })->name('task-api-images');
    });
});